import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',//必填
    name: 'home',//选填
    component: HomeView,
    meta: {
      isRoot: true,
      keep:true
    },
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue') //懒加载
  },
  {
    path: '/my',
    name: 'my',
    component: () => import('../views/my.vue') ,
    meta: {
      isRoot: true,
      keep:true
    },
  },
  {
    path: '/publish',
    name: 'publish',
    component: () => import('../views/publish.vue') ,
    meta: {
      isRoot: true,
      keep:true
    },
  },
  {
    path: '/article',
    name: 'article',
    component: () => import('../views/article/article.vue'),// 
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/search/search.vue')
  },
  {
    path: '/user_edit',
    name: 'user_edit',
    component: () => import('../views/userlnfo/user_edit.vue'),
    meta: {
      keep:true,
      auth:true
    },
  },
  {
    path: '/zhuce',
    name: 'zhuce',
    component: () => import('../views/zhuce.vue')
  },
  {
    path: '/wjmm',
    name: 'wjmm',
    component: () => import('../views/wjmm.vue')
  },
  {
    path: '/xgmm',
    name: 'xgmm',
    component: () => import('../views/xgmm.vue')
  },
  {
    path: '/lxwm',
    name: 'lxwm',
    component: () => import('../views/lxwm.vue')
  },
  {
    path: '/gywm',
    name: 'gywm',
    component: () => import('../views/gywm.vue')
  },
  {
    path: '/searchList',
    name: 'searchList',
    component: () => import('../views/search/list.vue'),// 
  },
]


const router = new VueRouter({
  routes
});
//全局前置守卫
import store from '@/store';
import {Toast} from 'vant'


router.beforeEach((to, from, next) => {
  // to:你准备去那个路由
  // from:从哪里来的
  let uid = store.state.uid;
  console.log(to.path);

  if(to.meta.auth){
    if(uid){
      //是登录状态
      next()
    }else{
      Toast('请先登录')
      setTimeout(v=>{
        router.replace('/Login')
      },1000) 
    }
  }else{
    next()
  }
  
 
})
export default router
